<?php

/**
 * @author zhuludong
 * @copyright 2018.9.10
 */

namespace Sjgame\Controller;
use Common\Controller\ApiController;
use Think\Model;

class LoginController extends ApiController {
    
    const GAME_TYPE = 'Sjgame';  // 游戏类型

     // 登陆接口
    public function login()
    {
        $code = I('code','','string');
        $nickname = I('nickname','','string');
        $avatar = I('avatar','','string');

        if(!$code){
           $this->response(1,'code不能为空');
        }

        $gameType = self::GAME_TYPE;
        $info = $this->wxAuthor($gameType,$code);

        $userId = isset($info['openid']) ? $info['openid'] : '';

        $map['userid'] = $userId;
        $dateline = date('Y-m-d H:i:s');
        $users = M('users')->where($map)->find();

        if(empty($users)) {
            $data = array('userid'=>$userId,'nickname'=>$nickname,'avatar'=>$avatar,'create_time'=>$dateline,'status'=>2);
            $ret = M('users')->add($data);
            // if($ret) {
            //     $this->countLog('pwzws',self::PWZW_AUTH_USER,1);  // 记录新增授权用户
            // }
        }else {
            M('users')->where($map)->save(array('nickname'=>$nickname,'avatar'=>$avatar));
        }

        $userInfo = M('users')->where($map)->find();
        $userInfo['serverTime'] = time();
        $this->response(0,'ok',$userInfo);
     
    }

    public function login2()
    {

        $appId = C('APPID');
        $appSecret = C('APPSECRET');
        $code = I('code','','string');
        
        $nickname = I('nickname','','string');
        $avatar = I('avatar','','string');

        if(!$code){
            $jsons['errno'] = 1;
            $jsons['msg'] = 'code不能为空';
            $this->ajaxReturn($jsons);
        }else if(empty($nickname)){
            $jsons['errno'] = 2;
            $jsons['msg'] = '昵称不能为空';
            $this->ajaxReturn($jsons);
        }else if(empty($avatar)){
            $jsons['errno'] = 3;
            $jsons['msg'] = '头像不能为空';
            $this->ajaxReturn($jsons);
        }
        
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code";
        
        $opts['http'] = array('method'=>'GET', 'timeout'=>5); // 设置超时5s
        $data = @file_get_contents($url, false, stream_context_create($opts)); 
        
        //$lastSql = '';
        $json2arr = $data ? @json_decode($data, true) : '';
        if(empty($json2arr['errcode'])) {
            $json = json_decode($data, true);
            
            $userId = isset($json['openid']) ? $json['openid'] : '';
            
            $map['userid'] = $userId;
            if(!$users = M('users')->where($map)->find()){
                $dateline = date('Y-m-d H:i:s');
                $ret = M('users')->add(array(
                    'userid'=>$userId,
                    'nickname'=>$nickname,
                    'avatar'=>$avatar,
                    'create_time'=>$dateline
                ));
                
                if($ret){
                    $users['userid'] = $userId;
                    $users['nickname'] = $nickname;
                    $users['avatar'] = $avatar;
                    $users['create_time'] = $dateline;
                }
            // 更改则更新用户信息    
            }else if($users['nickname'] != $nickname || $users['avatar'] != $avatar){
                M('users')->where($map)->save(array(
                    'nickname'=>$nickname,
                    'avatar'=>$avatar
                ));
            }
            $users['serverTime'] = time();
            // 如果用户存在则合并
            if($users){
                $data = array_merge($json2arr, $users); // 合并数组   
            }
        }
        
        $this->ajaxReturn($data);
    }

}